<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22
 * Time: 16:23
 */

namespace app\api\controller;

use app\api\Common;
use app\model\BbsCard;
use app\model\CardComment;
use app\model\BbsMain;
use app\model\CardCommentHf;
use app\model\CardCommentZan;
use app\model\CardZan;
use app\model\Fans;
use app\model\UserBbs;
use app\model\Userinfo;
use think\Db;
use think\Exception;
use think\Request;

//社区模块
class Community extends Common{


    /**
     * 发布帖子
     * post请求
     */
    public function release_card(){

        //用户id(uid)|帖子标题(title)|帖子详情(main_txt)|封面图(pic_h|pic_h2|pic_h3)
        $data = request()->post();
        $pic_h = uploadImageOne('pic_h');
        $pic_h2 = uploadImageOne('pic_h2');
        $pic_h3 = uploadImageOne('pic_h3');
        //判断用户上传的图片
        if($pic_h['status']==0){
            $data['pic_h'] = $pic_h['message'];
        }
        if($pic_h2['status']==0){
            $data['pic_h2'] = $pic_h2['message'];
    }
        if($pic_h3['status']==0){
            $data['pic_h3'] = $pic_h3['message'];
        }
        $data['title'] = urlencode($data['title']);
        $data['ctime'] = time();
        $data['ntime'] = time();
        $status = BbsCard::create($data);
        if($status){
            return_json(1,'发布成功');
        }else{
            return_json(0,'发布失败');
        }
    }


    /**
     * 帖子列表
     *  get请求
     */
    public function card_list(){

        //类型id(type_id 1为最新|2为关注) | 用户id(uid) 搜索条件(title)
        $uid = request()->get('uid');
        $type_id = \request()->get('type_id');
        $title = urlencode(\request()->get('title'));
        if(empty($type_id)){
            $type_id =1;
        }
        //获取关注用户
        $follow_uids = Fans::where('uid',$uid)->where('dstatus',1)->column('follow_uid');
        if($type_id==1){
            //获取最新帖子
            $card_list = BbsCard::where('dstatus',1)->where('title','like','%'.$title.'%')->with(['getUser'=>function($query){
                $query->field('id,username,head_portrait');
            }])->order('id desc')->paginate(10);

        }else{
            //获取关注的用户帖子
            $card_list = BbsCard::where('dstatus',1)->where('title','like','%'.$title.'%')->where('uid','in',$follow_uids)->with(['getUser'=>function($query){
                $query->field('id,username,head_portrait');
            }])->order('id desc')->paginate(10);
        }
        foreach ($card_list as $k => $v){
            $v['title'] = urldecode($v['title']);
            if(!empty($uid)){
                //查询用户点赞帖子id
                $zan_ids = CardZan::where('uid',$uid)->column('card_id');
                if(in_array($v['get_user']['id'],$follow_uids)){
                    $card_list[$k]['is_follow'] = 1;
                }else{
                    $card_list[$k]['is_follow'] = 0;
                }
                if(in_array($v['id'],$zan_ids)){
                    $card_list[$k]['is_zan'] = 1;
                }else{
                    $card_list[$k]['is_zan'] = 0;
                }
            }else{
                $details[$k]['is_zan'] = 0;
                $card_list[$k]['is_follow'] = 0;
            }
        }
        return_json(1,'获取数据成功',$card_list);
    }

    /**
     * 帖子点赞  H5
     */
    public function click_card_h5(){
        //获取用户id(uid)|帖子id(id)
        $uid = Request::instance()->param('uid');
        $id = Request::instance()->param('id');
//        dump($id);exit;
        $zan_id = CardZan::where(['uid'=>$uid,'card_id'=>$id])->value('id');
        if(!empty($zan_id)){
            //减少点赞记录
            $card = BbsCard::get($id);
            $card->num_like =  $card->num_like-1;  //点赞数减1
            if($card->save()){
                //删除点赞记录
                $status = CardZan::destroy($zan_id);
            }
            if($status){
                return_json(2,'取消成功');
            }
        }else{
            //增加点赞记录
            $card = BbsCard::get($id);
            $card->num_like =  $card->num_like+1;   //点赞数加1
            if($card->save()){
                //插入点赞记录
                $status = CardZan::create([
                    'uid'=>$uid,
                    'card_id'=>$id,
                    'ctime'=>time(),
                ]);
            }
            if($status){

                return_json(1,'点赞成功');
            }
        }

    }


    /**
     *  用户帖子点赞|帖子评论点赞
     *  post请求
     */
    public function click_card(){

        //获取用户id(uid)
        $uid = request()->post('uid');
        //获取点赞类型 type  1为帖子点赞 | 2为帖子评论点赞
        $type = \request()->post('type');
        //获取帖子id | 帖子评论id
        $id = request()->post('id');

        /**
         * 开启事物
         */
        Db::startTrans();
        try{
            if($type==1){
                //查询用户是否已经点赞该帖子
                $zan_id = CardZan::where(['uid'=>$uid,'card_id'=>$id])->value('id');
                if(!empty($zan_id)){
                    //减少点赞记录
                    $card = BbsCard::get($id);
                    $card->num_like =  $card->num_like-1;  //点赞数减1
                    if($card->save()){
                        //删除点赞记录
                        $status = CardZan::destroy($zan_id);
                    }
                }else{
                    //增加点赞记录
                    $card = BbsCard::get($id);
                    $card->num_like =  $card->num_like+1;   //点赞数加1
                    if($card->save()){
                        //插入点赞记录
                        $status = CardZan::create([
                            'uid'=>$uid,
                            'card_id'=>$id,
                            'ctime'=>time(),
                        ]);
                    }
                }
            }else{
                //查询用户是否已经点赞该评论
                $zan_id = CardCommentZan::where(['uid'=>$uid,'comment_id'=>$id])->value('id');
                if(!empty($zan_id)){
                    //减少点赞记录
                    $card = CardComment::get($id);
                    $card->like_num =  $card->like_num-1;  //点赞数减1
                    if($card->save()){
                        //删除点赞记录
                        $status = CardCommentZan::destroy($zan_id);
                    }
                }else{
                    //增加点赞记录
                    $card = CardComment::get($id);
                    $card->like_num =  $card->like_num+1;   //点赞数加1
                    if($card->save()){
                        //插入点赞记录
                        $status = CardCommentZan::create([
                            'uid'=>$uid,
                            'comment_id'=>$id,
                            'ctime'=>time(),
                        ]);
                    }
                }
            }

            /**
             * 提交事物
             */
            Db::commit();
            if($status){
                return_json(1,'操作成功');

            }

        }catch (\Exception $e){
            /**
             * 事物回滚
             */
            Db::rollback();
            return_json(0,'操作失败');
        }
    }


    /**
     * 用户评论帖子|评论用户评论|回复评论
     *  post请求
     */
    public function comment_reply(){

        //获取评论用户id(uid)|帖子id(card_id)|content(评论内容)|接收人id(to_uid)|评论id(comment_id)
        $uid = request()->post('uid');
        $card_id = request()->post('card_id');
        $content = urlencode(request()->post('content'));
        $to_uid = request()->post('to_uid');
        $comment_id = request()->post('comment_id');

        //判断是直接评论还是回复评论
        if(empty($comment_id)){
            //直接评论
            $status = CardComment::create([
                'uid'=>$uid,
                'card_id'=>$card_id,
                'content'=>$content,
                'ctime'=>time(),
                'ip'=>getip(),
            ]);
            $comment_list = [];
        }else{
            if(empty($to_uid)){
                $to_uid = '';
            }
            //回复评论
            $status = CardCommentHf::create([
                'uid'=>$uid,
                'to_uid'=>$to_uid,
                'comment_id'=>$comment_id,
                'card_id'=>$card_id,
                'content'=>$content,
                'ctime'=>time(),
                'ip'=>getip(),
            ]);
            //查询评论
            $comment_list = CardComment::where('id',$comment_id)->order('id desc')->select();
            if(!empty($comment_list)){
                foreach ($comment_list as $k => $v){
                    //判断用户是否点赞当前评论
                    if(!empty($uid)){
                        //查询用户点赞的评论
                        $comment_zan = CardCommentZan::where('uid',$uid)->column('comment_id');
                        if(in_array($v['id'],$comment_zan)){
                            $comment_list[$k]['is_zan'] = 1;
                        }else{
                            $comment_list[$k]['is_zan'] = 0;
                        }
                    }else{
                        $comment_list[$k]['is_zan'] = 0;
                    }
                    //获取用户头像|用户名
                    $comment_list[$k]['username'] = Userinfo::where('id',$v['uid'])->value('username');
                    $comment_list[$k]['head_portrait'] = Userinfo::where('id',$v['uid'])->value('head_portrait');
                    //查询回复评论
                    $comment_list_hf = CardCommentHf::where('comment_id',$v['id'])->select();
                    foreach ($comment_list_hf as  $key=>$val){
                        $val['content'] = urldecode($val['content']);
                        $comment_list_hf[$key]['from_name'] = Userinfo::where('id',$val['uid'])->value('username');
                        if(!empty($val['to_uid'])){
                            $comment_list_hf[$key]['to_name'] = Userinfo::where('id',$val['to_uid'])->value('username');
                        }else{
                            $comment_list_hf[$key]['to_name'] = '';
                        }
                        $comment_list_hf[$key]['time'] = date('Y-m-d',$comment_list_hf[$key]['ctime']);
                    }
                    $comment_list[$k]['hf'] = $comment_list_hf;

                    //1为有回复内容|2为没有回复内容
                    if(!empty($comment_list_hf)){
                        $comment_list[$k]['is_hf'] = 1;
                    }else{
                        $comment_list[$k]['is_hf'] = 2;
                    }
                }
            }

        }
        if($status){
            return_json(1,'评论成功',$comment_list);
        }else{
            return_json(0,'评论失败');
        }
    }

    /**
     * 帖子详情
     *  get请求   H5
     */
    public function card_details(){

        //获取客户端类型 1为安卓
        $headers = request()->header();
        if(empty($headers['pt'])){
            $headers['pt'] = 0;
        }
        //用户id(uid)|帖子id(card_id)
        $uid = request()->get('uid');
        $card_id = request()->get('card_id');
        //获取帖子详情
        $card_details = BbsCard::where('id',$card_id)->find();
        $card_details['title'] = urldecode($card_details['title']);
        $card_details['head_portrait'] = Userinfo::where('id',$card_details['uid'])->value('head_portrait');
        $card_details['username'] = Userinfo::where('id',$card_details['uid'])->value('username');
        $card_details['ctime'] = date('Y-m-d',$card_details['ctime']);
        if(!empty($uid)){
            //查询用户是否点赞该帖子
            $zan_id = CardZan::where(['uid'=>$uid,'card_id'=>$card_id])->value('id');
            if(!empty($zan_id)){
                $zan_id = '/static/community/images/2.png';
            }else{
                $zan_id = '/static/community/images/1.png';
            }
        }else{
            $zan_id = '/static/community/images/1.png';
            $uid = 0;
        }
//        dump($card_details);exit;
        //查询点赞该帖子的用户头像
        $user_ids = CardZan::where('card_id',$card_id)->limit(6)->column('uid');
        $user_photo = Userinfo::where('id','in',$user_ids)->select();
        return view('',['card_details'=>$card_details,'user_photo'=>$user_photo,'zan_id'=>$zan_id,'uid'=>$uid,'pt'=>$headers['pt']]);
    }

    /**
     * 帖子评论|回复列表
     *  get 请求
     */
    public function card_comment_list(){

        //获取用户id(uid)|帖子id(card_id)
        $card_id = request()->get('card_id');
        $uid = request()->get('uid');
        //查询评论
        $comment_list = CardComment::where('card_id',$card_id)->order('id desc')->paginate(10);
        if(!empty($comment_list)){
            foreach ($comment_list as $k => $v){
                $v['content'] = urldecode($v['content']);
                $comment_list[$k]['ctime'] = time_z($v['ctime']);
                //判断用户是否点赞当前评论
                if(!empty($uid)){
                    //查询用户点赞的评论
                    $comment_zan = CardCommentZan::where('uid',$uid)->column('comment_id');
                    if(in_array($v['id'],$comment_zan)){
                        $comment_list[$k]['is_zan'] = 1;
                    }else{
                        $comment_list[$k]['is_zan'] = 0;
                    }
                }else{
                    $comment_list[$k]['is_zan'] = 0;
                }
                //获取用户头像|用户名
                $comment_list[$k]['username'] = Userinfo::where('id',$v['uid'])->value('username');
                $comment_list[$k]['head_portrait'] = Userinfo::where('id',$v['uid'])->value('head_portrait');
                //查询回复评论
                $comment_list_hf = CardCommentHf::where('comment_id',$v['id'])->select();
                foreach ($comment_list_hf as  $key=>$val){
                    $val['content'] = urldecode($val['content']);
                    $comment_list_hf[$key]['from_name'] = Userinfo::where('id',$val['uid'])->value('username');
                    if(!empty($val['to_uid'])){
                        $comment_list_hf[$key]['to_name'] = Userinfo::where('id',$val['to_uid'])->value('username');
                    }else{
                        $comment_list_hf[$key]['to_name'] = '';
                    }
                    $comment_list_hf[$key]['time'] = date('Y-m-d',$comment_list_hf[$key]['ctime']);
                }
                $comment_list[$k]['hf'] = $comment_list_hf;

                //1为有回复内容|2为没有回复内容
                if(!empty($comment_list_hf)){
                    $comment_list[$k]['is_hf'] = 1;
                }else{
                    $comment_list[$k]['is_hf'] = 2;
                }
            }
        }
        return_json(1,'获取评论成功',$comment_list);
    }

    /**
     * 用户点击关注
     */
    public function click_follow(){

        //用户id(uid) | 关注用户id(follow_id)
        $uid = \request()->get('uid');
        $follow_id = \request()->get('follow_id');
        //查询用户是否已关注
        $is_follow = Fans::where('uid',$uid)->where('follow_uid',$follow_id)->find();
        if(!empty($is_follow)){
            //已经关注则为取消
            $is_follow->dstatus = $is_follow->dstatus == 1 ? 2:1;
            $status = $is_follow->save();
        }else{
            $status = Fans::create([
                'uid' =>  $uid,
                'follow_uid'=>$follow_id,
                'ctime'=>time(),
            ]);
        }
        if($status){
            return_json(1,'操作成功');
        }else{
            return_json(0,'操作失败');
        }
    }

}